
//
// var data= [{
//     "type": "Feature",
//     "properties": {
//         "type": "Gymnospermae",
//         "popupContent": "hh"
//     },
//     "geometry": {
//         "type": "Point",
//         "coordinates": [-104.99404, 39.75621]
//     }
// }, {
//     "type": "Feature",
//     "properties": {
//         "type": "Coniferopsida",
//         "popupContent": "hh"
//     },
//     "geometry": {
//         "type": "Point",
//         "coordinates": [-14.99404, 39.75621]
//     }
// }, {
//     "type": "Feature",
//     "properties": {
//         "type": "Bryophyta",
//

// var UIL="http://101.132.237.69:8080";
var UIL="http://127.0.0.1:8080/";

var type;
//
var data= [{
    "type": "Feature",
    "properties": {
        "type": "Gymnospermae",
        "popupContent": "hh"
    },
    "count":4,
    "geometry": {
        "type": "Point",
        "coordinates": [-104.99404, 39.75621]
    }
}, {
    "type": "Feature",
    "count":3,
    "properties": {
        "type": "Coniferopsida",
        "popupContent": "hh"
    },
    "geometry": {
        "type": "Point",
        "coordinates": [-14.99404, 39.75621]
    }
}, {
    "type": "Feature",
    "count":5,
    "properties": {
        "type": "Bryophyta",

    },
    "geometry": {
        "type": "Point",
        "coordinates": [-104.99404, 3.75621]
    }
}];

// var data=[-104.99404, 39.75621];
function start () {
    console.log("向后台发送请求");
    // $.ajax({
    //     type: "get",
    //     url: UIL + "/homepage/location",
    //     success: function (data) {
    //         console.log("成功",data);
    //         Draw(data);
    //     },
    //     error: function () {
    //         console.log("失败");
    //     }
    // });
    Draw1(data);
}


var classes=new Array();
function StartSecondLabel(data) {

//移除除底图外的所有图层
    map.eachLayer(function (layer) {
        if (!layer._container || ('' + jQuery(layer._container).attr('class')).replace(/\s/g, '') != 'leaflet-layer') {
            layer.remove();
        }
    });
    let index;
    for(index in classes)
    {
        map.removeLayer(classes[index])
        console.log("删除图层")
    }
    var colors =new Map();
    var arr=new Array();
    var Data=new Array();
    for (index in data) {
        if (!colors.has(data[index].properties.type)) {
            var cl= getColor2();
            colors.set(data[index].properties.type, cl);
            arr.push(data[index].properties.type);
        }
    }
    // console.log("arr",arr)
    for( i=arr.length-1;i>=0;i--)
    {
        var Arr=new Array();
        for (index in data) {
            if(data[index].properties.type==arr[i])
            {
                Arr.push(data[index])
            }
        }
        Data.push(Arr);
    }

    // console.log("Data",Data)
    var overlayMaps = {};

    for (index in Data) {
        function geojsonMarkerOptions(feature) {
            return {
                fillColor: colors.get(feature.properties.type),
                weight: 2,
                opacity: 1,
                color: 'white',
                //dashArray: '3',
                fillOpacity: 0.7
            };
        }
        var class1 = L.geoJSON(Data[index],
            {
                onEachFeature: onEachFeature,
                pointToLayer: function (feature, latlng) {
                    return L.circleMarker(latlng, geojsonMarkerOptions(feature));
                }
            });
        var name= Data[index][0].properties.type;
        overlayMaps[name]=class1;
    }
    var overlayMap=L.control.layers(overlayMaps);
    classes.push (overlayMap)
    overlayMap.addTo(map)
}

function Search1(type1) {
    type=type1;
    $.ajax({
        type: "get",
        url: UIL + "/search"+"/"+type1,
        // data:
        //     {
        //         "type": type
        //     },
        success: function (data) {
            StartSecondLabel(data);
            console.log(data)
        },
        error: function () {
            console.log("失败");
        }
    });
}

//随机颜色
function getColor2() {
    var color = "#";
    for (var i = 0; i < 6; i++) {
        color += (Math.random() * 16 | 0).toString(16);
    }
    return color;
}

//点击显示信息
function onEachFeature(feature, layer) {
    // does this feature have a property named popupContent?
    // console.log("feature")
    // console.log(feature)
    if (feature.properties ) {
        layer.bindPopup("Phylum: "+feature.properties.phylum+"<br/>"
                + "Class: "+feature.properties.class+"<br/>"
                + "Order: "+feature.properties.order+"<br/>"
                + "Family: "+feature.properties.family+"<br/>"
                + "<text  style='font-style:italic;font-weight:bold'>"
                + "Genus: "+feature.properties.genus+"<br/>"
                + "Species: "+feature.properties.species+"<br/>"+"</text>"
                + "Start_year: "+feature.properties.start_year+"<br/>"
                + "End_year: "+feature.properties.end_year+"<br/>"
                + "Feature: "+feature.geometry.coordinates);
        // if(type=="Phylum"){
        //     layer.bindPopup("<font color='#FF69B4'>"+"Phylum: "+feature.properties.phylum+"</font>"+"<br/>"+
        //         "Class: "+feature.properties.class+"<br/>"
        //         + "Order: "+feature.properties.order+"<br/>"
        //         + "Family: "+feature.properties.family+"<br/>"
        //         + "Genus: "+feature.properties.genus+"<br/>"
        //         + "Species: "+feature.properties.species+"<br/>"
        //         + "Start_year: "+feature.properties.start_year+"<br/>"
        //         + "End_year: "+feature.properties.end_year+"<br/>"
        //         + "Feature: "+feature.geometry.coordinates);
        // }
        // else if(type=="Class"){
        //     layer.bindPopup("Phylum: "+feature.properties.phylum+"<br/>"+
        //         "<font color='#FF69B4'>"+ "Class: "+feature.properties.class+"</font>"+"<br/>"
        //         + "Order: "+feature.properties.order+"<br/>"
        //         + "Family: "+feature.properties.family+"<br/>"
        //         + "Genus: "+feature.properties.genus+"<br/>"
        //         + "Species: "+feature.properties.species+"<br/>"
        //         + "Start_year: "+feature.properties.start_year+"<br/>"
        //         + "End_year: "+feature.properties.end_year+"<br/>"
        //         + "Feature: "+feature.geometry.coordinates);
        // }
        // else if(type=="order"){
        //     layer.bindPopup("Phylum: "+feature.properties.phylum+"<br/>"
        //         + "Class: "+feature.properties.class+"<br/>"
        //         + "<font color='#FF69B4'>"
        //         + "Order: "+feature.properties.order+"<br/>"+"</font>"
        //         + "Family: "+feature.properties.family+"<br/>"
        //         + "Genus: "+feature.properties.genus+"<br/>"
        //         + "Species: "+feature.properties.species+"<br/>"
        //         + "Start_year: "+feature.properties.start_year+"<br/>"
        //         + "End_year: "+feature.properties.end_year+"<br/>"
        //         + "Feature: "+feature.geometry.coordinates);
        // }
        // else if(type=="Genus"){
        //     layer.bindPopup("Phylum: "+feature.properties.phylum+"<br/>"
        //         + "Class: "+feature.properties.class+"<br/>"
        //         + "Order: "+feature.properties.order+"<br/>"
        //         + "Family: "+feature.properties.family+"<br/>"
        //         + "<font color='#FF69B4'  font-style='italic'>"
        //         + "Genus: "+feature.properties.genus+"<br/>"+"</font>"
        //         + "Species: "+feature.properties.species+"<br/>"
        //         + "Start_year: "+feature.properties.start_year+"<br/>"
        //         + "End_year: "+feature.properties.end_year+"<br/>"
        //         + "Feature: "+feature.geometry.coordinates);
        // }
        // else if(type=="Species"){
        //     layer.bindPopup("Phylum: "+feature.properties.phylum+"<br/>"
        //         + "Class: "+feature.properties.class+"<br/>"
        //         + "Order: "+feature.properties.order+"<br/>"
        //         + "Family: "+feature.properties.family+"<br/>"
        //         + "Genus: "+feature.properties.genus+"<br/>"
        //         + "<font color='#FF69B4' font-style='italic'>"
        //         + "Species: "+feature.properties.species+"<br/>"+"</font>"
        //         + "Start_year: "+feature.properties.start_year+"<br/>"
        //         + "End_year: "+feature.properties.end_year+"<br/>"
        //         + "Feature: "+feature.geometry.coordinates);
        // }
        // else{
        //     layer.bindPopup("Phylum: "+feature.properties.phylum+"<br/>"
        //         + "Class: "+feature.properties.class+"<br/>"
        //         + "Order: "+feature.properties.order+"<br/>"
        //         + "Family: "+feature.properties.family+"<br/>"
        //         + "Genus: "+feature.properties.genus+"<br/>"
        //         + "Species: "+feature.properties.species+"<br/>"
        //         + "Start_year: "+feature.properties.start_year+"<br/>"
        //         + "End_year: "+feature.properties.end_year+"<br/>"
        //         + "Feature: "+feature.geometry.coordinates);
        // }



    }
}


function Draw1(geojsonFeature ) {

    function geojsonMarkerOptions(feature) {
        return {
            fillColor: "#777",
            weight: 2,
            opacity: 1,
            color: 'white',
            fillOpacity: 0.7
        };
    }

    let i=0;
    L.geoJSON(geojsonFeature,
        {
            onEachFeature: onEachFeature,
            pointToLayer: function (feature, latlng) {

                let htmlStr = '<p><div class="map-circle">'
                    +'<span >' + geojsonFeature[i].count+ '</span>'
                    +'</div><p/>';
                let icon = L.divIcon({
                    html: htmlStr,
                    iconSize: [80, 80],
                    className: ''
                })
                i++;
                // return L.circleMarker(latlng, geojsonMarkerOptions(feature));
                return  L.marker(latlng, {icon: icon})
            }
        }).addTo(map);
    console.log(geojsonFeature)
}

//
//      //
//      // var legend = L.control({position: 'topright'});
//      // legend.onAdd = function (map) {//onAdd函数向其中添加组件
//      //     var div = L.DomUtil.create('div', 'info legend'),
//      //         grades = ["Gymnospermae", "Bryophyta","Coniferopsida"],
//      //         labels = [];
//      //     // loop through our density intervals and generate a label with a colored square for each interval
//      //     for (var i = 0; i < grades.length; i++) {
//      //         div.innerHTML +=
//      //             '<i style="background:' + getColor(grades[i] ) + '"></i> ' +
//      //             grades[i] + (grades[i ] ? '' + '<br>' : '+');
//      //     }
//      //     return div;
//      // };
//      //
//      // legend.addTo(map);//加入显示比例尺
//  }



